#!/usr/bin/env bash

SELFDIR=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)

#   $1  compile
function cmd_compile()
{
    cd "${SELFDIR}/src" && go build -o "${SELFDIR}/bin/sago"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "Build 'sago' failed(${RESULT})"
        return  1
    fi

    echo    "Build 'sago' success: '${SELFDIR}/bin/sago'"
    return ${RESULT}
}

#   $1  install
#   $2  TargetDir
function cmd_install()
{
    local target_dir="$2"
    return 0
}

function cmd_help()
{
    echo    "Usage:"
    echo    "    sh build.sh -h|--help|help"
    echo    "    sh build.sh [compile]"
    echo    "    sh build.sh install [TargetDir]"

    return  0
}

function main()
{
    local action="$1"
    if [[ "${action}" == "" ]]; then
        action="compile"
    fi

    # 分发命令
    case "${action}" in
        -h|--help|help)
            cmd_help "$@"
            return  $?
            ;;
        compile)
            cmd_compile "$@"
            return  $?
            ;;
        install)
            cmd_install "$@"
            return  $?
            ;;
    esac

    echo "Unsupported command '${action}'"
    return  1
}

main    "$@"
exit    "$?"
